{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-07-12T02:23:42.282847Z",
     "iopub.status.busy": "2021-07-12T02:23:42.282358Z",
     "iopub.status.idle": "2021-07-12T02:23:42.343830Z",
     "shell.execute_reply": "2021-07-12T02:23:42.343234Z",
     "shell.execute_reply.started": "2021-07-12T02:23:42.282806Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "from keras.models import Sequential\n",
    "from keras.layers import Conv2D, MaxPooling2D\n",
    "from keras.layers import Activation, Dropout, Flatten, Dense\n",
    "from keras.optimizers import Adam\n",
    "\n",
    "model = Sequential()\n",
    "model.add(Conv2D(16, (5, 5), padding='same', input_shape=(48, 48, 3)))\n",
    "model.add(Activation('relu'))\n",
    "model.add(MaxPooling2D(pool_size=(2, 2)))\n",
    "\n",
    "model.add(Conv2D(32, (5, 5), padding='same', ))\n",
    "model.add(Activation('relu'))\n",
    "model.add(MaxPooling2D(pool_size=(2, 2)))\n",
    "\n",
    "model.add(Conv2D(32, (5, 5), padding='same', ))\n",
    "model.add(Activation('relu'))\n",
    "model.add(MaxPooling2D(pool_size=(2, 2)))\n",
    "\n",
    "model.add(Flatten())\n",
    "model.add(Dense(64))\n",
    "model.add(Activation('relu'))\n",
    "model.add(Dropout(0.5))\n",
    "model.add(Dense(7, activation=\"softmax\"))\n",
    "# model.add(Activation('softmax'))\n",
    "\n",
    "model.compile(loss='categorical_crossentropy',\n",
    "              optimizer=Adam(learning_rate=0.001),\n",
    "              metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-07-12T02:23:42.763972Z",
     "iopub.status.busy": "2021-07-12T02:23:42.763411Z",
     "iopub.status.idle": "2021-07-12T02:23:42.775129Z",
     "shell.execute_reply": "2021-07-12T02:23:42.774532Z",
     "shell.execute_reply.started": "2021-07-12T02:23:42.763923Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential_4\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "conv2d_11 (Conv2D)           (None, 48, 48, 16)        1216      \n",
      "_________________________________________________________________\n",
      "activation_17 (Activation)   (None, 48, 48, 16)        0         \n",
      "_________________________________________________________________\n",
      "max_pooling2d_11 (MaxPooling (None, 24, 24, 16)        0         \n",
      "_________________________________________________________________\n",
      "conv2d_12 (Conv2D)           (None, 24, 24, 32)        12832     \n",
      "_________________________________________________________________\n",
      "activation_18 (Activation)   (None, 24, 24, 32)        0         \n",
      "_________________________________________________________________\n",
      "max_pooling2d_12 (MaxPooling (None, 12, 12, 32)        0         \n",
      "_________________________________________________________________\n",
      "conv2d_13 (Conv2D)           (None, 12, 12, 32)        25632     \n",
      "_________________________________________________________________\n",
      "activation_19 (Activation)   (None, 12, 12, 32)        0         \n",
      "_________________________________________________________________\n",
      "max_pooling2d_13 (MaxPooling (None, 6, 6, 32)          0         \n",
      "_________________________________________________________________\n",
      "flatten_4 (Flatten)          (None, 1152)              0         \n",
      "_________________________________________________________________\n",
      "dense_8 (Dense)              (None, 64)                73792     \n",
      "_________________________________________________________________\n",
      "activation_20 (Activation)   (None, 64)                0         \n",
      "_________________________________________________________________\n",
      "dropout_4 (Dropout)          (None, 64)                0         \n",
      "_________________________________________________________________\n",
      "dense_9 (Dense)              (None, 7)                 455       \n",
      "=================================================================\n",
      "Total params: 113,927\n",
      "Trainable params: 113,927\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-07-12T02:23:43.504441Z",
     "iopub.status.busy": "2021-07-12T02:23:43.503931Z",
     "iopub.status.idle": "2021-07-12T02:23:44.435103Z",
     "shell.execute_reply": "2021-07-12T02:23:44.433880Z",
     "shell.execute_reply.started": "2021-07-12T02:23:43.504395Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found 22968 images belonging to 7 classes.\n",
      "Found 5741 images belonging to 7 classes.\n"
     ]
    }
   ],
   "source": [
    "batch_size = 16\n",
    "\n",
    "from keras.preprocessing.image import ImageDataGenerator\n",
    "train_datagen = ImageDataGenerator(rescale=1./255,\n",
    "                                   # rotation_range=10,\n",
    "                                   # width_shift_range=0.2,\n",
    "                                   # height_shift_range=0.2,\n",
    "                                   # horizontal_flip=True,\n",
    "                                   validation_split=0.2)\n",
    "\n",
    "val_datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2)\n",
    "\n",
    "train_data = train_datagen.flow_from_directory('./Datawhale_人脸情绪识别_数据集/train/', \n",
    "                                               color_mode='rgb',\n",
    "                                               batch_size=batch_size, \n",
    "                                               class_mode='categorical',\n",
    "                                               target_size=(48, 48),\n",
    "                                               shuffle=True,\n",
    "                                               subset = 'training') \n",
    "\n",
    "val_data = val_datagen.flow_from_directory('./Datawhale_人脸情绪识别_数据集/train/', \n",
    "                                           color_mode='rgb',\n",
    "                                           batch_size=batch_size, \n",
    "                                           class_mode='categorical',\n",
    "                                           target_size=(48, 48),\n",
    "                                           shuffle=False,\n",
    "                                           subset = 'validation')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-07-12T02:23:44.437586Z",
     "iopub.status.busy": "2021-07-12T02:23:44.437089Z",
     "iopub.status.idle": "2021-07-12T02:24:11.593749Z",
     "shell.execute_reply": "2021-07-12T02:24:11.593302Z",
     "shell.execute_reply.started": "2021-07-12T02:23:44.437541Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/5\n",
      "1436/1436 [==============================] - 6s 4ms/step - loss: 1.8434 - accuracy: 0.2410 - val_loss: 1.6486 - val_accuracy: 0.4384\n",
      "Epoch 2/5\n",
      "1436/1436 [==============================] - 5s 4ms/step - loss: 1.6569 - accuracy: 0.3452 - val_loss: 1.4604 - val_accuracy: 0.4281\n",
      "Epoch 3/5\n",
      "1436/1436 [==============================] - 5s 4ms/step - loss: 1.5086 - accuracy: 0.4159 - val_loss: 1.3834 - val_accuracy: 0.4753\n",
      "Epoch 4/5\n",
      "1436/1436 [==============================] - 5s 4ms/step - loss: 1.4250 - accuracy: 0.4439 - val_loss: 1.3349 - val_accuracy: 0.4853\n",
      "Epoch 5/5\n",
      "1436/1436 [==============================] - 5s 4ms/step - loss: 1.3625 - accuracy: 0.4729 - val_loss: 1.2874 - val_accuracy: 0.4822\n"
     ]
    }
   ],
   "source": [
    "model.fit_generator(\n",
    "        train_data,\n",
    "        epochs=5,\n",
    "        validation_data=val_data,\n",
    "        validation_steps=200)\n",
    "model.save_weights('first_try.h5') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-07-12T02:28:33.877360Z",
     "iopub.status.busy": "2021-07-12T02:28:33.876799Z",
     "iopub.status.idle": "2021-07-12T02:28:41.727508Z",
     "shell.execute_reply": "2021-07-12T02:28:41.726797Z",
     "shell.execute_reply.started": "2021-07-12T02:28:33.877312Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found 35887 images belonging to 2 classes.\n",
      "WARNING:tensorflow:Your input ran out of data; interrupting training. Make sure that your dataset or generator can generate at least `steps_per_epoch * epochs` batches (in this case, 35887 batches). You may need to use the repeat() function when building your dataset.\n"
     ]
    }
   ],
   "source": [
    "test_datagen = ImageDataGenerator(rescale=1./255)\n",
    "\n",
    "test_generator = test_datagen.flow_from_directory(\n",
    "        './Datawhale_人脸情绪识别_数据集/',\n",
    "        target_size=(48, 48),\n",
    "        color_mode=\"rgb\",\n",
    "        shuffle = False,\n",
    "        class_mode='categorical',\n",
    "        batch_size=10)\n",
    "\n",
    "filenames = test_generator.filenames\n",
    "nb_samples = len(filenames)\n",
    "predict = model.predict_generator(test_generator,steps = nb_samples)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-07-12T02:32:32.776729Z",
     "iopub.status.busy": "2021-07-12T02:32:32.776208Z",
     "iopub.status.idle": "2021-07-12T02:32:32.783406Z",
     "shell.execute_reply": "2021-07-12T02:32:32.782659Z",
     "shell.execute_reply.started": "2021-07-12T02:32:32.776682Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'angry': 0,\n",
       " 'disgusted': 1,\n",
       " 'fearful': 2,\n",
       " 'happy': 3,\n",
       " 'neutral': 4,\n",
       " 'sad': 5,\n",
       " 'surprised': 6}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_data.class_indices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-07-12T02:34:07.921969Z",
     "iopub.status.busy": "2021-07-12T02:34:07.921397Z",
     "iopub.status.idle": "2021-07-12T02:34:07.927770Z",
     "shell.execute_reply": "2021-07-12T02:34:07.926824Z",
     "shell.execute_reply.started": "2021-07-12T02:34:07.921922Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "cls_name = np.array(['angry', 'disgusted', 'fearful', 'happy','neutral', 'sad', 'surprised'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-07-12T02:34:37.701709Z",
     "iopub.status.busy": "2021-07-12T02:34:37.701142Z",
     "iopub.status.idle": "2021-07-12T02:34:37.714763Z",
     "shell.execute_reply": "2021-07-12T02:34:37.714030Z",
     "shell.execute_reply.started": "2021-07-12T02:34:37.701660Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "submit_df = pd.DataFrame({'name': filenames, 'label': cls_name[predict.argmax(1)]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-07-12T02:34:42.596408Z",
     "iopub.status.busy": "2021-07-12T02:34:42.595853Z",
     "iopub.status.idle": "2021-07-12T02:34:42.607805Z",
     "shell.execute_reply": "2021-07-12T02:34:42.607166Z",
     "shell.execute_reply.started": "2021-07-12T02:34:42.596362Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>test/00001.png</td>\n",
       "      <td>fearful</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>test/00002.png</td>\n",
       "      <td>sad</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>test/00003.png</td>\n",
       "      <td>happy</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>test/00004.png</td>\n",
       "      <td>fearful</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>test/00005.png</td>\n",
       "      <td>happy</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             name    label\n",
       "0  test/00001.png  fearful\n",
       "1  test/00002.png      sad\n",
       "2  test/00003.png    happy\n",
       "3  test/00004.png  fearful\n",
       "4  test/00005.png    happy"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "submit_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-07-12T02:36:02.852150Z",
     "iopub.status.busy": "2021-07-12T02:36:02.851613Z",
     "iopub.status.idle": "2021-07-12T02:36:02.864403Z",
     "shell.execute_reply": "2021-07-12T02:36:02.863719Z",
     "shell.execute_reply.started": "2021-07-12T02:36:02.852101Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "submit_df = submit_df[submit_df['name'].apply(lambda x: 'test' in x )]\n",
    "submit_df['name'] = submit_df['name'].apply(lambda x: x.split('/')[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-07-12T02:36:32.127278Z",
     "iopub.status.busy": "2021-07-12T02:36:32.126679Z",
     "iopub.status.idle": "2021-07-12T02:36:32.144232Z",
     "shell.execute_reply": "2021-07-12T02:36:32.143736Z",
     "shell.execute_reply.started": "2021-07-12T02:36:32.127230Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "submit_df = submit_df.sort_values(by='name')\n",
    "submit_df.to_csv('submit.csv', index=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
